//
//  huiyuanDetailsViewController.m
//  daogouApp
//
//  Created by panquanReabam.com on 15-6-2.
//  Copyright (c) 2015年 jiezsoft.com. All rights reserved.
//

#import "AddMemberViewController.h"
#import "HttpRequest.h"
#import "TagView.h"
#import "UIViewExt.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"

#import "HZAreaPickerView.h"
#import "ZJSwitch.h"

#import "MemberListViewController.h"
#import "GenerateQRCodeViewController.h"
#import "SelectTag_NewViewController.h"
#import "AddMemberView.h"
#import "TagModel.h"
@interface AddMemberViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UIScrollViewDelegate,UITextViewDelegate,HZAreaPickerDelegate,AddMemberViewDelegate,GenerateQRCodeViewControllerDelegate>
@property (strong, nonatomic) NSString *areaValue;
@property (strong, nonatomic) NSString*cityValue;
@property (strong, nonatomic) HZAreaPickerView *locatePicker;
@property (nonatomic,retain)NSString*provinceStr;
@property (nonatomic,retain)NSString*cityStr;
@property (nonatomic,retain)NSString*districtStr;
-(void)cancelLocatePicker;

@end

@implementation AddMemberViewController
{
    NSMutableArray*selectTagCodeArray;
    NSMutableArray*selectTagNameArray;
    AddMemberView*MemberView;
    NSMutableArray* arrrs;
    BOOL saveAccount;


}
@synthesize alt = alt;
@synthesize areaValue=_areaValue, cityValue=_cityValue;
@synthesize locatePicker=_locatePicker;

-(void)setAreaValue:(NSString *)areaValue
{
    if (![_areaValue isEqualToString:areaValue]) {
        
       MemberView. regionTextField.text = areaValue;
        }
}
-(void)setCityValue:(NSString *)cityValue
{
    if (![_cityValue isEqualToString:cityValue]) {
         MemberView. regionTextField.text = cityValue;
    }
}
#pragma mark - HZAreaPicker delegate
-(void)pickerDidChaneStatus:(HZAreaPickerView *)picker
{
    if (picker.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
        self.areaValue = [NSString stringWithFormat:@"%@ %@ %@", picker.locate.state, picker.locate.city, picker.locate.district];
        self.provinceStr= picker.locate.state;
        self.cityStr=picker.locate.city;
        self.districtStr=picker.locate.district;
        } else{
        self.cityValue = [NSString stringWithFormat:@"%@ %@", picker.locate.state, picker.locate.city];
        
    }
}
-(void)area:(NSString*)str{
      
    [self cancelLocatePicker];
    self.locatePicker = [[HZAreaPickerView alloc] initWithStyle:HZAreaPickerWithStateAndCityAndDistrict delegate:self] ;
    [self.locatePicker showInView:self.view];
    MemberView.regionTextField.text=@"北京 通州";
    self.provinceStr=@"北京";
    self.cityStr=@"通州";
}
-(void)cancelLocatePicker:(NSString*)str{
    [self cancelLocatePicker];
}
-(void)cancelLocatePicker
{
    [self.locatePicker cancelPicker];
    self.locatePicker.delegate = nil;
    self.locatePicker = nil;
}
#pragma mark - TextField delegate

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self cancelLocatePicker];
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated

{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavi];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    selectTagCodeArray=[[NSMutableArray alloc]init];
    self.title = @"添加会员";
    UIButton*editor = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0, 35, 35)];
    [editor setTitle:@"保存" forState:UIControlStateNormal];
    editor.titleLabel.font=[UIFont systemFontOfSize:16];
    [editor setTitleColor:luseColor forState:UIControlStateNormal];
    [editor addTarget:self action:@selector(editor:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:editor];
    self.navigationItem.rightBarButtonItem = myBtnBtn;
    MemberView = [[AddMemberView alloc]initWithFrame:self.view.bounds];
    MemberView.mDelegate=self;
    ZJSwitch *switch2 = [[ZJSwitch alloc] initWithFrame:CGRectMake(ScreenWidth-75, 98/2+10+44, 55, ScreenHeight*15/1334)];
    switch2.onText = @"男";
    switch2.offText = @"女";
    if (![self.sex isEqual:[NSNull null]]) {
        if ([self.sex isEqualToString:@"1"]) {
            switch2.on = YES;
            switch2.backgroundColor = [UIColor clearColor];
            switch2.onTintColor = [UIColor colorWithRed:53/255.0 green:159/255.0 blue:234/255.0 alpha:1];
            switch2.tintColor = [UIColor colorWithRed:253.00/255.0 green:129.00/255.0 blue:182.00/255.0 alpha:1];
            self.sex= @"男";
            
        }else {
            switch2.on = NO;
            switch2.backgroundColor = [UIColor clearColor];
            switch2.onTintColor = [UIColor colorWithRed:53/255.0 green:159/255.0 blue:234/255.0 alpha:1];
            switch2.tintColor = [UIColor colorWithRed:253.00/255.0 green:129.00/255.0 blue:182.00/255.0 alpha:1];
            self.sex= @"女";
        }
    }else {
        switch2.on = NO;
        switch2.backgroundColor = [UIColor clearColor];
        switch2.onTintColor = [UIColor colorWithRed:53/255.0 green:159/255.0 blue:234/255.0 alpha:1];
        switch2.tintColor = [UIColor colorWithRed:253.00/255.0 green:129.00/255.0 blue:182.00/255.0 alpha:1];
        self.sex= @"女";
        
    }
    [switch2 addTarget:self action:@selector(switchAction1:) forControlEvents:UIControlEventValueChanged];
    [MemberView.myscrollView addSubview:switch2];
    
    [self.view addSubview:MemberView];
    
}
-(void)tagBtn:(NSString*)str{
    SelectTag_NewViewController *detailViewController = [[SelectTag_NewViewController alloc] init];
    detailViewController.optionNameString=@"MemberTag";
    detailViewController.selectedTagCodeArray=selectTagCodeArray;
    detailViewController.selectedTagNameArray=selectTagNameArray;
    [self.navigationController pushViewController:detailViewController animated:YES];
}
-(void)switchAction1:(UISwitch*)sender{
    
    if (sender.on ==YES) {
        self.sex=nil;
        self.sex=@"男";
    }
    if (sender.on == NO) {
        self.sex=nil;
        self.sex=@"女";
    }
}

-(void)editor:(UIButton*)sender{
    if ([MemberView. userNameTextFied.text length] < 1)
        {
            [self showAlertMessage:@"请输入会员姓名"];
        }
        else
        {
            if ([MemberView. phoneTextFied.text length] <1)
            {
                [self showAlertMessage:@"请输入手机号码"];
            }
            else{
          
                        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                        NSString* Token = [user objectForKey:@"tokenId"];
                        NSMutableDictionary *params = [NSMutableDictionary new];
                        [params setValue:Token forKey:@"tokenId"];
                        [params setValue:MemberView.userNameTextFied.text forKey:@"userName"];
                        [params setValue:self.nickName forKey:@"nickName"];
                        [params setValue:self.provinceStr forKey:@"provinceName"];
                        [params setValue:self.cityStr forKey:@"cityName"];
                        [params setValue:self.districtStr forKey:@"regionName"];
                        [params setValue:self.openID forKey:@"openId"];
                        [params setValue:MemberView.addressTextFied.text forKey:@"address"];
                        [params setValue:MemberView.phoneTextFied.text forKey:@"phone"];
                        [params setValue:self.headImgUrl forKey:@"headImage"];
                        NSMutableArray *mDocumentLine=[NSMutableArray array];
                for (int i=0; i<selectTagCodeArray.count; i++) {
                    
                            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                            NSString *codeStr = [selectTagCodeArray objectAtIndex:i];
                            [dic setValue:codeStr forKey:@"code"];
                            [mDocumentLine addObject:dic];
                    }
              
                        NSLog(@"====%@",mDocumentLine);
                        [params setValue:mDocumentLine  forKey:@"tags"];
                        [params setValue:self.sex forKey:@"sex"];
                        [params setValue:MemberView. noteView.text forKey:@"memo"];
                    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
                    [HttpRequest requestData:@"Business/Member/Add" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                            if (isSuccess) {
                                
                                 NSLog(@"33333333====%@",severDataDict.dataDict);
                                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                                    [Global hideProgressHUDInternal:self.view];
                                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                                    [Global showTextHudSub:requestResultStr view:self.view];
                                    [self.navigationController popViewControllerAnimated:YES];
                      
                                }else{
                                    [Global hideProgressHUDInternal:self.view];
                                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                                    [Global showTextHudSub:requestResultStr view:self.view];
                                }
                                
                            }
                            else
                            {
                                [Global hideProgressHUDInternal:self.view];
                                
                                [Global showTextHudSub:@"连接失败" view:self.view];
                            }
                            
                        }];
            }}
}
- (NSData *)toJSONData:(id)theData{
    
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    if ([jsonData length] > 0 && error == nil){
        return jsonData;
    }else{
        return nil;
    }
}

- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

-(void)setNavi{
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    selectTagNameArray=[NSMutableArray arrayWithObjects:@"你好",@"你好",@"你好",@"你好",@"你好",@"你好",@"你好",@"你好",@"你好",@"你好",@"你好",@"你好", nil];
    TagModel *dataModel=[TagModel dataModelWithUserDict:selectTagNameArray];
    MemberView.model=dataModel;
}

-(void)secondViewController:(GenerateQRCodeViewController *)secondViewController headImgUrl:(NSString *)headImgUrl nickName:(NSString *)nickName openID:(NSString *)openID sex:(NSString *)sex{

    MemberView.weChatIdTextFied.text=nickName;
    self.headImgUrl=headImgUrl;
    self.nickName=nickName;
    self.openID=openID;
    self.sex=sex;

}
-(void)QrCodeBtn:(NSString *)str{
    GenerateQRCodeViewController*detailViewController = [[GenerateQRCodeViewController alloc] init];
    detailViewController.QrCode=@"GetReadMemberQrCode";
    detailViewController.JumpStatus=@"AddView";
    detailViewController.mDelegate=self;
    [self.navigationController pushViewController:detailViewController animated:YES];
    
}

@end
